/*
 * Copyright 2017 Baiyyy
 * @(#)WarningController.java
 */

package com.byyy.ccts.modules.cc.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializeFilter;
import com.byyy.ccts.core.common.controller.BaseCRUDController;
import com.byyy.ccts.core.model.AjaxJson;
import com.byyy.ccts.core.model.PageJson;
import com.byyy.ccts.core.query.data.PropertyPreFilterable;
import com.byyy.ccts.core.query.data.Queryable;
import com.byyy.ccts.core.query.utils.QueryableConvertUtils;
import com.byyy.ccts.core.query.wrapper.EntityWrapper;
import com.byyy.ccts.core.security.shiro.authz.annotation.RequiresPathPermission;
import com.byyy.ccts.core.utils.DateUtils;
import com.byyy.ccts.core.utils.StringUtils;
import com.byyy.ccts.modules.cc.entity.ccts.Warning;
import com.byyy.ccts.modules.cc.service.IWarningService;
import com.byyy.ccts.modules.common.annotation.LogRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by DELL on 2018-8-21.
 */
@Controller
@RequestMapping("${admin.url.prefix}/cc/warning")
@RequiresPathPermission("cc:warning")
public class WarningController extends BaseCRUDController<Warning, String> {

    @Autowired
    private IWarningService warningService;

    /**
     * 根据页码和每页记录数，自定义查询预警列表信息
     *
     * @param request
     * @param response
     * @throws java.io.IOException
     */
    @RequestMapping(value = "warningList", method = { RequestMethod.GET, RequestMethod.POST })
    public void warningList(Queryable queryable, PropertyPreFilterable propertyPreFilterable, HttpServletRequest request,
                          HttpServletResponse response) throws IOException {
        EntityWrapper<Warning> entityWrapper = new EntityWrapper<Warning>(entityClass);
        preAjaxList(queryable, entityWrapper, request, response);
        // 预处理
        QueryableConvertUtils.convertQueryValueToEntityValue(queryable, entityClass);
        SerializeFilter filter = propertyPreFilterable.constructFilter(entityClass);
        PageJson<Warning> pagejson = new PageJson<Warning>(warningService.warningList(queryable, entityWrapper));
        String content = JSON.toJSONString(pagejson, filter);
        StringUtils.printJson(response, content);
    }

    /**
     * 修改温度记录页面
     * @param model
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value="updateEdit", method = RequestMethod.GET)
    public String updateEdit(Model model, HttpServletRequest request, HttpServletResponse response) {
        model.addAttribute("deviceId", request.getParameter("deviceId"));
        model.addAttribute("sn", request.getParameter("sn"));
        model.addAttribute("collectTime", request.getParameter("collectTime"));
        model.addAttribute("temperature", request.getParameter("temperature"));
        model.addAttribute("wendMin", request.getParameter("wendMin"));
        model.addAttribute("wendMax", request.getParameter("wendMax"));
        return display("updateEdit");
    }

    /**
     * 修改温度
     * @return
     */
    @ResponseBody
    @RequestMapping(value="updateTemp", method = RequestMethod.POST)
    @LogRecord(title = "修改温度")
    public AjaxJson updateTemp(String deviceId, String sn, String collectTime, Float temperature, Float temperatureBefore) {
        AjaxJson json = new AjaxJson();
        Warning warning = new Warning();
        warning.setSbNo(deviceId);
        warning.setSn(sn);
        warning.setCollectTime(DateUtils.parseTimestamp(collectTime));
        warning.setTemperature(temperature);
        this.warningService.updateTemp(warning, temperatureBefore);
        return json;
    }

}
